import { Component, OnInit, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent, FileUpload } from 'primeng/primeng';
import {
    DeviceServiceProxy,
    CostOfDeviceListDto,
    CreateOrUpdateCostOfDeviceInput
} from '@shared/service-proxies/service-proxies';
import * as moment from 'moment';
import { BsDatepickerViewMode } from 'ngx-bootstrap/datepicker/models';
import { BsDatepickerConfig, BsDaterangepickerConfig } from 'ngx-bootstrap';
import { CreateOrEditLedgerModalComponent } from './create-or-edit-ledger-modal.component';
import { CreateOrEditConsumablesModalComponent } from '../consumables/create-or-edit-consumables-modal.component';

@Component({
    templateUrl: './ledger.component.html',
    animations: [appModuleAnimation()]
})
export class LedgerComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('ExcelFileUpload') excelFileUpload: FileUpload;
    // @ViewChild('excelModal') excelModal: ExcelModalComponent;
    @ViewChild('createOrEditLedgerModal') createOrEditLedgerModal: CreateOrEditLedgerModalComponent;
    @ViewChild('createOrEditConsumablesModal') createOrEditConsumablesModal: CreateOrEditConsumablesModalComponent;

    anyncing = false;
    filterText = '';
    deviceName = '';
    deviceNumber = '';
    sorting = '';

    public dateRange: Date[] = [
        moment()
            .startOf('day')
            .toDate(),
        moment()
            .endOf('day')
            .toDate()
    ];
    // userDataPermissions;
    // selectedDepts;
    selectedValues = new Array<CostOfDeviceListDto>();
    minMode: BsDatepickerViewMode = 'month';

    bsConfig: Partial<BsDaterangepickerConfig>;
    //extensions: Array<ListDtoExtensionsItem>;
    constructor(injector: Injector, private _deviceServiceProxy: DeviceServiceProxy) {
        super(injector);
    }

    ngOnInit() {}

    getTableData(event?: LazyLoadEvent) {
        // if (!this.selectedDepts) {
        //     return;
        // }
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);

            return;
        }
        this.filterText = '';
        this.deviceNumber = '';
        this.deviceName = '';
        this.primengTableHelper.showLoadingIndicator();
        //this.selectedValues = new Array<CostOfDeviceListDto>();
        this._deviceServiceProxy
            .getAllAsync(
                this.filterText,
                this.deviceName,
                this.deviceNumber,
                this.sorting,

                // this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.hideLoadingIndicator();
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }

    getAllAsync(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);

            return;
        }

        this.primengTableHelper.showLoadingIndicator();

        this._deviceServiceProxy
            .getAllAsync(
                this.filterText,
                this.deviceName,
                this.deviceNumber,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }

    createOrEditLedger(): void {
        this.createOrEditLedgerModal.show();
    }
    createOrEditConsumables(): void {
        this.createOrEditConsumablesModal.show();
    }

    selectedDelete() {
        this.message.confirm(this.l('AreYouSure'), isConfirmed => {
            if (isConfirmed) {
                this.selectedValues.forEach(element => {
                    this._deviceServiceProxy.deleteAsync(element.id).subscribe(() => {
                        this.notify.success(this.l('SuccessfullyDeleted'));
                        this.getTableData();
                    });
                });
            }
        });
    }

    delete(costOfDevice: CostOfDeviceListDto): void {
        let self = this;
        self.message.confirm('确认删除该条数据吗？', isConfirmed => {
            if (isConfirmed) {
                this._deviceServiceProxy.deleteAsync(costOfDevice.id).subscribe(() => {
                    abp.notify.success(this.l('SuccessfullyDeleted'));
                    self.getAllAsync();
                });
            }
        });
    }
}
